package jsm.comsuner;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import jms.connection.JmsConnection;

public class JmsConsumer {
	private Connection conn;
//	private Destination des;
	private Session session;
	private MessageConsumer consumer;
	
	public JmsConsumer(){
		conn = JmsConnection.getInstance().createConnection();
		try {
			conn.start();
			session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
			Destination destination = session.createQueue("books");
			consumer = session.createConsumer(destination);
		} catch (JMSException e) {
			e.printStackTrace();
		}
		
	}
	
	public String consume(){
		TextMessage message;
		try {
			message = (TextMessage) consumer.receive();
			return message.getText();
		} catch (JMSException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public void close(){
		try {
			consumer.close();
			session.close();
			conn.close();
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
	
}
